package com.yonyou.iuap.activiti.utils;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * @author guoxh alias 灰太狼(wolffy)
 * @Date 2019/7/3 11:11
 * @desc com.yonyou.iuap.activiti.utils
 *
 * activiti 流程常用封装
 */
@Component
public class ActivitiUtil {

    @Resource(name="processEngine")
    private ProcessEngine processEngine;

    /**
     *  根据zip包加载流程
     * @param file zip包
     * @param processName 流程名称
     * @throws FileNotFoundException
     */
    public void deployProcess(File file, String processName) throws FileNotFoundException {
        InputStream is = new FileInputStream(file);
        ZipInputStream zipInputStream = new ZipInputStream(is);
        processEngine.getRepositoryService().createDeployment()
                .addZipInputStream(zipInputStream)
                .name(processName)
                .deploy();
    }

    /**
     * 根据字节流来进行流程部署
     * @param is
     * @param processName
     */
    public void deployProcessByInputStream(InputStream is,String processName){
        ZipInputStream zipInputStream = new ZipInputStream(is);
        processEngine.getRepositoryService().createDeployment()
                .name(processName)
                .addZipInputStream(zipInputStream)
                .deploy();
    }


    /**
     * 查询所有的部署流程
     * @return
     */
    public List<Deployment> queryAllDeployment(){
        List<Deployment> list = processEngine.getRepositoryService().createDeploymentQuery()
                .orderByDeploymenTime()
                .desc()
                .list();
        return list;
    }

    /**
     * 查询所有的流程部署定义信息
     * @return
     */
    public List<ProcessDefinition> queryAllProcessInstance(){
        List<ProcessDefinition> processDefinitions = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        return processDefinitions;
    }

    /**
     *根据流程部署id,删除流程
     * @param deploymentId
     */
    public void deleteDeploymentByPid(String deploymentId){
        //act_re_deployment 表的id;
        // 是否级联删除 true/false
        processEngine.getRepositoryService().deleteDeployment(deploymentId,true);
    }

    /**
     * 根据processDefinitionId 查看流程图
     * @param pid
     * @return
     */
    public InputStream lookProcessImage(String pid){
        //processDefinitionId => act_re_procdef 的id字段.
        return processEngine.getRepositoryService().getProcessDiagram(pid);
    }

    /**
     *
     * @param billId
     * @param userId
     */
    public void startProcessInstance(String billId,String userId){
       Map<String,Object> variable = new HashMap<>();
       variable.put("userID",userId);
       //第一个参数就是流程中自己定义的名字,一定要匹配,否则找不到.
       processEngine.getRuntimeService().startProcessInstanceByKey("shenqingtest," + billId,variable);
    }
}
